package com.example.moveclock;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.AnalogClock;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {
	static final String STARTS_COUNT = "startsCount";
	int startsCount = 0;
	@Override
	public void onSaveInstanceState(Bundle savedInstanceState) {
	    // Save the user's current game state
	    savedInstanceState.putInt(STARTS_COUNT, startsCount + 1);
	    super.onSaveInstanceState(savedInstanceState);
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		if (savedInstanceState != null) {
			startsCount = savedInstanceState.getInt(STARTS_COUNT);
	    }
		TextView textViewStartsCount = (TextView) findViewById(R.id.textViewStartsCount);
		textViewStartsCount.setText("Starts: " + startsCount);
		
	}
	
	@Override
	public void onRestoreInstanceState(Bundle savedInstanceState) {
	    // Always call the superclass so it can restore the view hierarchy
	    super.onRestoreInstanceState(savedInstanceState);
	    if (savedInstanceState != null) {
			startsCount = savedInstanceState.getInt(STARTS_COUNT);
	    }
		TextView textViewStartsCount = (TextView) findViewById(R.id.textViewStartsCount);
		textViewStartsCount.setText("Restarts: " + startsCount);
		
	    // Restore state members from saved instance
	    savedInstanceState.putInt(STARTS_COUNT, startsCount);
	}
	
	@Override
	public void onResume(){
		super.onResume();
		//TODO: remove the following method 
		moveArrow();
	}

	private void moveArrow() {
		
		android.os.Debug.stopMethodTracing();

		ImageView arrow = (ImageView) findViewById(R.id.imageArrow1);
		
		AnimationSet aset = new AnimationSet(false);

		Animation rotation = new RotateAnimation(180, 220);
		rotation.setDuration(3000);
		rotation.setStartOffset(5000);
		
		Animation animation = new TranslateAnimation(getRelativeLeft(arrow),
				getRelativeLeft(arrow) + 100,
				getRelativeTop(arrow),
				getRelativeTop(arrow) + 100);
		animation.setDuration(5000);
		

		
		aset.addAnimation(rotation);
		aset.addAnimation(animation);
		
		arrow.startAnimation(aset);
	}
	
	private int getRelativeLeft(View myView) {
	    if (myView.getParent() == myView.getRootView())
	        return myView.getLeft();
	    else
	        return myView.getLeft() + getRelativeLeft((View)myView.getParent());
	}

	private int getRelativeTop(View myView) {
	    if (myView.getParent() == myView.getRootView())
	        return myView.getTop();
	    else
	        return myView.getTop() + getRelativeTop((View)myView.getParent());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
	    switch (item.getItemId()) {
	        case R.id.menu_item_exit:
	        	this.finish();
	            return true;
	        case R.id.menu_settings:
	            //showHelp();
	            return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}

	public void up(View view) {
		AnalogClock clock = (AnalogClock) findViewById(R.id.analogClock1);
		Log.d("upmethod", "clock.getTop() " + clock.getTop());
		clock.layout(clock.getLeft(), 
				clock.getTop() - 10, 
				clock.getRight(), 
				clock.getBottom() - 10);
	}

	public void down(View view) {
		
		AnalogClock clock = (AnalogClock) findViewById(R.id.analogClock1);
		clock.layout(clock.getLeft(), 
				clock.getTop() + 10, 
				clock.getRight(), 
				clock.getBottom() + 10);
	}

	public void left(View view) {
		AnalogClock clock = (AnalogClock) findViewById(R.id.analogClock1);
		clock.layout(clock.getLeft() - 10, 
				clock.getTop(), 
				clock.getRight() - 10, 
				clock.getBottom());
	}

	public void right(View view) {
		AnalogClock clock = (AnalogClock) findViewById(R.id.analogClock1);
		clock.layout(clock.getLeft() + 10, 
				clock.getTop(), 
				clock.getRight() + 10, 
				clock.getBottom());
		
	}
	
	public void exit(View view) {
		Log.d("upmethod", "perform exit");
		onDestroy();
		closeContextMenu();
		closeOptionsMenu();
		MainActivity.this.finish();
	}
}
